/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huaweicloud.rds.sdk.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.rds.v3.RdsClient;
import com.huaweicloud.sdk.rds.v3.model.CreatePostgresqlDbUserRequest;
import com.huaweicloud.sdk.rds.v3.model.CreatePostgresqlDbUserResponse;
import com.huaweicloud.sdk.rds.v3.model.ListPostgresqlDbUserPaginatedRequest;
import com.huaweicloud.sdk.rds.v3.model.ListPostgresqlDbUserPaginatedResponse;
import com.huaweicloud.sdk.rds.v3.model.PostgresqlUserForCreation;
import com.huaweicloud.sdk.rds.v3.region.RdsRegion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * rds(postgresql)数据库用户列表相关示例代码
 * 1、查看数据库用户列表
 * 2、创建数据库用户
 * 3、查看数据库用户列表
 *
 * @since 2022-10-17
 */
public class RdsDbUserDemo {
    private static final Logger logger = LoggerFactory.getLogger(RdsDbUserDemo.class.getName());

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String instanceId = "<YOUR INSTANCE_ID>";
        String name = "<YOUR NAME>";
        String password = "<YOUR PASSWORD>";
        int page = 1; // <YOUR PAGE>
        int limit = 10; // <YOUR LIMIT>

        RdsClient client = createClient(ak, sk);
        // 查看数据库用户列表
        listPostgresqlDbUserPaginated(client, instanceId, page, limit);
        // 创建数据库用户
        createPostgresqlDbUser(client, instanceId, name, password);
        // 查看数据库用户列表
        listPostgresqlDbUserPaginated(client, instanceId, page, limit);
    }

    private static RdsClient createClient(String ak, String sk) {
        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);
        return RdsClient.newBuilder().withCredential(auth).withRegion(RdsRegion.CN_EAST_3).build();
    }

    private static void listPostgresqlDbUserPaginated(RdsClient client, String instanceId, int page, int limit) {
        ListPostgresqlDbUserPaginatedRequest request = new ListPostgresqlDbUserPaginatedRequest();
        request.withInstanceId(instanceId);
        request.withPage(page);
        request.withLimit(limit);
        try {
            ListPostgresqlDbUserPaginatedResponse response = client.listPostgresqlDbUserPaginated(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("listPostgresqlDbUserPaginated ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("listPostgresqlDbUserPaginated RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("listPostgresqlDbUserPaginated httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
                e.getErrorMsg());
        }
    }

    private static void createPostgresqlDbUser(RdsClient client, String instanceId, String name, String password) {
        CreatePostgresqlDbUserRequest request = new CreatePostgresqlDbUserRequest();
        request.withInstanceId(instanceId);
        PostgresqlUserForCreation body = new PostgresqlUserForCreation();
        body.withName(name);
        body.withPassword(password);
        request.withBody(body);
        try {
            CreatePostgresqlDbUserResponse response = client.createPostgresqlDbUser(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("createPostgresqlDbUser ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("createPostgresqlDbUser RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("createPostgresqlDbUser httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
                e.getErrorMsg());
        }
    }

}
